package org.sc.learn.server.auth.ctrl;

import org.sc.learn.server.auth.repository.AuthorityRepository;
import org.sc.learn.server.auth.repository.RoleRepository;
import org.sc.learn.server.auth.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;

/**
 * Created by qwx89 on 2018/3/28.
 */
@RestController
public class AuthCtrl {

  @Autowired
  private UserRepository userRepository;
  @Autowired
  private RoleRepository roleRepository;
  @Autowired
  private AuthorityRepository authorityRepository;

  /**
   * 初始化用户数据
   */
//  @Autowired
//  public void init() {
//    authorityRepository.deleteAll();
//    roleRepository.deleteAll();
//    userRepository.deleteAll();
//    Set<Authority> authorities = new HashSet<>();
//    Authority a1 = new Authority();
//    a1.setName("manager");
//    a1.setValue("permit:all");
//    authorities.add(a1);
//    Authority a2 = new Authority();
//    a2.setName("testuser");
//    a2.setValue("permit:test");
//    authorities.add(a2);
//    authorityRepository.save(authorities);
//
//    Set<Role> roles = new HashSet<>();
//    Role r1 = new Role();
//    r1.setName("manager");
//    r1.setValue("manager");
//    r1.setAuthorities(Sets.newHashSet(a1));
//    roles.add(r1);
//    Role r2 = new Role();
//    r2.setName("test");
//    r2.setValue("test");
//    r2.setAuthorities(Sets.newHashSet(a2));
//    roles.add(r2);
//    roleRepository.save(roles);
//
//
//    List<User> users = new ArrayList<>();
//    User u1 = new User();
//    u1.setUsername("admin");
//    u1.setPassword("123456");
//    u1.setRoles(Sets.newHashSet(r1));
//    users.add(u1);
//    User u2 = new User();
//    u2.setUsername("test");
//    u2.setPassword("123456");
//    u2.setRoles(Sets.newHashSet(r2));
//    users.add(u2);
//    userRepository.save(users);
//  }

  @GetMapping("user")
  public Principal user(Principal user) {
    return user;
  }
}
